package ryarithsGame;

//import java.util.logging.Logger;

import javax.swing.ImageIcon;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.input.AbsoluteMouse;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.MouseInput;
import com.jme.input.NodeHandler;
import com.jme.light.DirectionalLight;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.CameraNode;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.Skybox;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.scene.Spatial.TextureCombineMode;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jmex.terrain.TerrainBlock;
import com.jmex.terrain.util.MidPointHeightMap;
import com.jmex.terrain.util.ProceduralTextureGenerator;


public class Game extends SimpleGame{

        /**
         * @param args
         */
        
 //       private static final Logger logger = Logger.getLogger(Game.class.getName());
        private static String openingPic = "data/images/DragonGames.png";
        
        private TerrainBlock tb;
        private CameraNode cameraNode;
        private Skybox skybox;
        private Fence fence1;
        private AbsoluteMouse am;
        private Vector3f newLoc;
        private Aimer aimer;
        private Bullets bullets;
        private float wait = 0f;
        private Text text;
        private Node level;
        private Level0 gameLevel0;
        private Level1 gameLevel1;
        private Level2 gameLevel2;
        private Level3 gameLevel3;
        private Level4 gameLevel4;
        private Level5 gameLevel5;
        private Level6 gameLevel6;
        private Level7 gameLevel7;
        private Vector2f camLocPos;
        private int currLevel;
        private String status;
        private boolean levelComplete, gameOver, isStuck;
        KeyBindingManager keyboard;

        
        public static void main(String[] args) 
        {
                Game main = new Game();
                main.setConfigShowMode(ConfigShowMode.AlwaysShow, Game.class
                                .getClassLoader().getResource(openingPic));
                main.start();
        }

        @Override
        protected void simpleInitGame() 
        {
        	    newLoc = new Vector3f();
                buildTerrain();
                buildCamera();
                buildInput();
                buildLighting();
                buildSkyBox();
                buildFenceBox();
                buildAimer();
                buildBullets();
                generateGameLevels();
                
                level.attachChild(gameLevel0);
                cameraNode.setLocalTranslation(new Vector3f(50f, (tb.getHeight(50f,150f)+5), 150f));
                cameraNode.lookAt(new Vector3f(150f, 20, 150), new Vector3f(0,1,0));
                rootNode.attachChild(level);
                rootNode.updateRenderState();
                
        }
                 
        private void MouseLogic()
    	{
    		if(wait >= 0)
    		{
    			wait -= tpf;
    		}
    		if (MouseInput.get().isButtonDown(0)) 
    		{
    			if(wait < 0)
    			{
    			System.out.println("FIRE");
    			bullets.Fire(cam.getLocation(), cam.getDirection(), cameraNode.getLocalRotation());
      			wait = .1f;
    			rootNode.updateRenderState();
    			}
    		}
    	}
        
        
        
        private void generateGameLevels()
        {
        	//numLevels = 7;
        	currLevel = 0;
        	gameOver = false;
        	level = new Node("level");
        	gameLevel0 = new Level0();
            gameLevel1 = new Level1();
            gameLevel2 = new Level2();
            gameLevel3 = new Level3();
            gameLevel4 = new Level4();
            gameLevel5 = new Level5();
            gameLevel6 = new Level6();
            gameLevel7 = new Level7();
            
        }
        
        private void buildCamera()
        {
        	cameraNode = new CameraNode("Camera Node", cam);
            cameraNode.setLocalTranslation(new Vector3f(312.5f, (tb.getHeight(312.5f,312.5f)+5), 312.5f));
            cameraNode.updateWorldData(0);
            rootNode.attachChild(cameraNode);
        }
        
        private void buildBullets()
        {
        	 bullets = new Bullets("bullets", rootNode);
             rootNode.attachChild(bullets);
             bullets.setLightCombineMode(LightCombineMode.Inherit);
        }
        
        private void buildInput()
        {
      
            input = new NodeHandler(cameraNode, 50f, .5f);
            am = new AbsoluteMouse("Mouse Input", display.getWidth(), display.getHeight());
            am.registerWithInputHandler(input);
            keyboard = KeyBindingManager.getKeyBindingManager();
            keyboard.set("nextLevel", KeyInput.KEY_L);
            keyboard.set("cont", KeyInput.KEY_Z);
        }
        
        private void keyboardLogic()
        {
        	if(keyboard.isValidCommand("nextLevel", true))
    		{
    			if(levelComplete)
    			{
    				currLevel++;
    				levelComplete = false;
    				nextLevel();
    			}
    		}
        	
        	if(keyboard.isValidCommand("cont", true))
        	{
        		levelComplete = true;
        	}
        }
        
        private void buildAimer()
        {
        	aimer = new Aimer("aimer");
            aimer.setLocalTranslation(new Vector3f(0,0,0));
            rootNode.attachChild(aimer);
            
            text = Text.createDefaultTextLabel("Test Label", "");
            text.setCullHint(Spatial.CullHint.Never);
            text.setTextureCombineMode(TextureCombineMode.Replace);
            text.setLocalTranslation(new Vector3f(1, 60, 0));
    		
            Text cross = Text.createDefaultTextLabel("Cross hairs", "+");
            cross.setCullHint(Spatial.CullHint.Never);
            cross.setTextureCombineMode(TextureCombineMode.Replace);
            cross.setLocalTranslation(new Vector3f(
    				display.getWidth() / 2f - 8f, // 8 is half the width
    														// of a font char
    				display.getHeight() / 2f - 8f, 0));

            statNode.attachChild(text);
            statNode.attachChild(cross);
            
            MousePick pick = new MousePick(cam, rootNode, text);
    		input.addAction(pick);
        }
        
        private void buildTerrain() 
        {
                
                
        MidPointHeightMap heightMap = new MidPointHeightMap(64, 1f);
        // Scale the data
        Vector3f terrainScale = new Vector3f(10, 0.0575f, 10);
        // create a terrainblock
         tb = new TerrainBlock("Terrain", heightMap.getSize(), terrainScale,
                heightMap.getHeightMap(), new Vector3f(0, 0, 0));

        tb.setModelBound(new BoundingBox());
        tb.updateModelBound();

        // generate a terrain texture with 2 textures
        ProceduralTextureGenerator pt = new ProceduralTextureGenerator(
                heightMap);
        pt.addTexture(new ImageIcon(Game.class.getClassLoader()
                .getResource("data/textures/grassb.png")), -128, 0, 128);
        pt.addTexture(new ImageIcon(Game.class.getClassLoader()
                .getResource("data/textures/dirt.jpg")), 0, 128, 255);
        pt.addTexture(new ImageIcon(Game.class.getClassLoader()
                .getResource("data/textures/highest.jpg")), 128, 255,
                384);
        pt.createTexture(32);
        
        // assign the texture to the terrain
        TextureState ts = display.getRenderer().createTextureState();
        Texture t1 = TextureManager.loadTexture(pt.getImageIcon().getImage(),
                Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, true);
        ts.setTexture(t1, 0);

        tb.setRenderState(ts);
        tb.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
        rootNode.attachChild(tb);
        
        }
        
        private void buildLighting() {
                /** Set up a basic, default light. */
            DirectionalLight light = new DirectionalLight();
            light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
            light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
            light.setDirection(new Vector3f(1,-1,0));
            light.setEnabled(true);

              /** Attach the light to a lightState and the lightState to rootNode. */
            LightState lightState = display.getRenderer().createLightState();
            lightState.setEnabled(true);
            lightState.attach(light);
            rootNode.setRenderState(lightState);
        }

        private void buildSkyBox() {
        skybox = new Skybox("skybox", 10, 10, 10);

        Texture north = TextureManager.loadTexture(
            Game.class.getClassLoader().getResource(
            "data/textures/north1.jpg"),
            Texture.MinificationFilter.BilinearNearestMipMap,
            Texture.MagnificationFilter.Bilinear);
        Texture south = TextureManager.loadTexture(
            Game.class.getClassLoader().getResource(
            "data/textures/south1.jpg"),
            Texture.MinificationFilter.BilinearNearestMipMap,
            Texture.MagnificationFilter.Bilinear);
        Texture east = TextureManager.loadTexture(
            Game.class.getClassLoader().getResource(
            "data/textures/east1.jpg"),
            Texture.MinificationFilter.BilinearNearestMipMap,
            Texture.MagnificationFilter.Bilinear);
        Texture west = TextureManager.loadTexture(
            Game.class.getClassLoader().getResource(
            "data/textures/west1.jpg"),
            Texture.MinificationFilter.BilinearNearestMipMap,
            Texture.MagnificationFilter.Bilinear);
        Texture up = TextureManager.loadTexture(
            Game.class.getClassLoader().getResource(
            "data/textures/top1.jpg"),
            Texture.MinificationFilter.BilinearNearestMipMap,
            Texture.MagnificationFilter.Bilinear);
        Texture down = TextureManager.loadTexture(
            Game.class.getClassLoader().getResource(
            "data/textures/bottom1.jpg"),
            Texture.MinificationFilter.BilinearNearestMipMap,
            Texture.MagnificationFilter.Bilinear);

        skybox.setTexture(Skybox.Face.North, north);
        skybox.setTexture(Skybox.Face.West, west);
        skybox.setTexture(Skybox.Face.South, south);
        skybox.setTexture(Skybox.Face.East, east);
        skybox.setTexture(Skybox.Face.Up, up);
        skybox.setTexture(Skybox.Face.Down, down);
        skybox.preloadTextures();
        
        rootNode.attachChild(skybox);
        }

    	private void buildFenceBox()
    	{
    		fence1 = new Fence("fence1",312.5f,312.5f,625f);
    		rootNode.attachChild(fence1);
    	}
        
        protected void simpleUpdate()
        {
        		
        		updateCam();
          		updateLevel();
        		updateEnv();
                MouseLogic();
                keyboardLogic();
                bullets.updateShots(tpf);
                status = "Incomplete";
                if(levelComplete)
                	status = "Complete      [Press 'L' to continue]";
                if(gameOver)
                	status = "GameOver [Press 'ESC' to exit]";
                text.print("Level: " + currLevel + "  Status: " + status);
        }
        
        private void updateCam()
        {
        	newLoc.set(cam.getLocation());
        	if(cam.getLocation().x >= 625 || cam.getLocation().z >= 625 || cam.getLocation().x <= 1 || cam.getLocation().z <= 1)
    		{
    			
        		newLoc.set(cam.getLocation().subtract(cam.getDirection().clone().mult(3f)));
        		System.out.println(""+newLoc);
    			
    		}    		
    		newLoc.setY((tb.getHeight(newLoc.x,newLoc.z)+5));
    		
    		rootNode.getChild(cameraNode.getName()).setLocalTranslation(newLoc);
    		rootNode.getChild(aimer.getName()).setLocalTranslation(cam.getLocation().add(cam.getDirection().clone().mult(2f)));
            rootNode.getChild(aimer.getName()).setLocalRotation(cameraNode.getLocalRotation());
        }
        
        private void updateEnv()
        {
        	rootNode.getChild(skybox.getName()).setLocalTranslation(cam.getLocation());
            
            fence1.updateFence(timer.getTimePerFrame());
         	
        }
        
        private void nextLevel()
        {
        	if(currLevel == 1)
        	{
        		gameLevel0.detachAll();
        		rootNode.detachChild(gameLevel0);
        		rootNode.attachChild(gameLevel1);
                cameraNode.setLocalTranslation(new Vector3f(312.5f, (tb.getHeight(312.5f,312.5f)+5), 312.5f));
        	}
        	if(currLevel == 2)
        	{
        		isStuck = true;
        		gameLevel1.detachAllChildren();
        		rootNode.detachChild(gameLevel1);
        		rootNode.attachChild(gameLevel2);
                cameraNode.setLocalTranslation(new Vector3f(312.5f, (tb.getHeight(312.5f,312.5f)+5), 312.5f));
        	}
        	if(currLevel == 3)
        	{
        		isStuck = true;
        		gameLevel2.detachAllChildren();
        		rootNode.detachChild(gameLevel2);
        		rootNode.attachChild(gameLevel3);
                cameraNode.setLocalTranslation(new Vector3f(100, (tb.getHeight(100,100)+5), 100));
        	}
        	if(currLevel == 4)
        	{
        		 node1 = true;
        	     node2 = true;
        	     node3 = true;
        	     node0 = true;

        		gameLevel3.detachAllChildren();
        		rootNode.detachChild(gameLevel3);
        		rootNode.attachChild(gameLevel4);
        		for(int i = 0; i < gameLevel4.puzzleSize(); i++)
        		{
        			gameLevel4.getPuzzleNode(i).setShowFence(false);
        		}
                cameraNode.setLocalTranslation(new Vector3f(312.5f, (tb.getHeight(312.5f,312.5f)+5), 312.5f));
        	}
        	if(currLevel == 5)
        	{
        		gameLevel4.detachAllChildren();
        		rootNode.detachChild(gameLevel4);
        		rootNode.attachChild(gameLevel5);
        		gameLevel5.getGoalNode().setShowSwitch(false);
        		gameLevel5.getPuzzleNode(8).setShowFence(false);
        		gameLevel5.getPuzzleNode(8).setShowSwitch(false);        		
                cameraNode.setLocalTranslation(new Vector3f(25f, (tb.getHeight(25f,25f)+5), 25f));
        	}
        	if(currLevel == 6)
        	{
        		gameLevel5.detachAllChildren();
        		rootNode.detachChild(gameLevel5);
        		rootNode.attachChild(gameLevel6);
        		gameLevel6.getPuzzleNode(0).setShowFence(false);
        		gameLevel6.getPuzzleNode(1).setShowFence(false);
        		gameLevel6.getPuzzleNode(12).setShowFence(false);
        		gameLevel6.getPuzzleNode(13).setShowFence(false);
   
        		gameLevel6.getPuzzleNode(0).setShowSwitch(false);
        		gameLevel6.getPuzzleNode(1).setShowSwitch(false);
        		gameLevel6.getPuzzleNode(12).setShowSwitch(false);
        		gameLevel6.getPuzzleNode(13).setShowSwitch(false);

        		cameraNode.lookAt(new Vector3f(-1,20,-1), new Vector3f(0,1,0));
                cameraNode.setLocalTranslation(new Vector3f(15f, (tb.getHeight(15f,15f)+5), 15f));
        	}
        	if(currLevel == 7)
        	{
        		levelComplete = true;
        		gameLevel6.detachAllChildren();
        		rootNode.detachChild(gameLevel6);
        		rootNode.attachChild(gameLevel7);
        		gameOver = true;
        		cameraNode.setLocalTranslation(new Vector3f(50f, (tb.getHeight(50f,150f)+5), 150f));
                cameraNode.lookAt(new Vector3f(150f, 20, 150), new Vector3f(0,1,0));
        	}
        	if(currLevel == 8)
        	{
        		gameLevel7.detachAllChildren();
        		rootNode.detachChild(gameLevel7);
                cameraNode.setLocalTranslation(new Vector3f(312.5f, (tb.getHeight(312.5f,312.5f)+5), 312.5f));
                gameOver = true;
                finish();
        	}
        	
        	rootNode.updateRenderState();
        }
        
        private void updateLevel()
        {
        	   	
        	if(currLevel == 0)
        		updateLevel0();
        	if(currLevel == 1)
        		updateLevel1();
        	if(currLevel == 2)
        		updateLevel2();
        	if(currLevel == 3)
        		updateLevel3();
        	if(currLevel == 4)
        		updateLevel4();
        	if(currLevel == 5)
        		updateLevel5();
        	if(currLevel == 6)
        		updateLevel6();
        	if(currLevel == 7)
        		updateLevel7();
        }
        
        private void updateLevel1()
        {
        	camLocPos = new Vector2f(cam.getLocation().x, cam.getLocation().z);
        	
        	for(int i = 0; i < gameLevel1.puzzleSize(); i++)
        	{
        		if(gameLevel1.getPuzzleNode(i).getType() == 1)
        		{
        			gameLevel1.getPuzzleNode(i).bulletShots(bullets.CheckCollisions(new BoundingBox(gameLevel1.getPuzzleNode(i).getSwitchLoc(), 5, 5,5)));
        		}
        		
        		if(gameLevel1.getPuzzleNode(i).checkFenceCollisions(camLocPos))
        		{
        			newLoc.set(cam.getLocation().subtract(cam.getDirection().clone().mult(3f)));
        		}
        		if(gameLevel1.getPuzzleNode(i).getType() == 0)
        		{
        			gameLevel1.getPuzzleNode(i).checkSwitchCollisions(camLocPos);
        		}
        	}
        	gameLevel1.checkSwitchCollisions(new Vector2f(cam.getLocation().x, cam.getLocation().z));
        	gameLevel1.updateLevel(timer.getTimePerFrame());
        	
        	if(gameLevel1.getGoalSwitch())
        	{
        		levelComplete = true;
        	}
        	
        }
        
        private void updateLevel0()
        {
        	camLocPos = new Vector2f(cam.getLocation().x, cam.getLocation().z);
        	
        	for(int i = 0; i < gameLevel0.puzzleSize(); i++)
        	{
        		if(gameLevel0.getPuzzleNode(i).getType() == 1)
        		{
        			gameLevel1.getPuzzleNode(i).bulletShots(bullets.CheckCollisions(new BoundingBox(gameLevel0.getPuzzleNode(i).getSwitchLoc(), 5, 5,5)));
        		}
        		
        		if(gameLevel0.getPuzzleNode(i).checkFenceCollisions(camLocPos))
        		{
        			newLoc.set(cam.getLocation().subtract(cam.getDirection().clone().mult(3f)));
        		}
        		if(gameLevel0.getPuzzleNode(i).getType() == 0)
        		{
        			gameLevel0.getPuzzleNode(i).checkSwitchCollisions(camLocPos);
        		}
        	}
        	gameLevel0.checkSwitchCollisions(new Vector2f(cam.getLocation().x, cam.getLocation().z));
        	gameLevel0.updateLevel(timer.getTimePerFrame());
        	
        	if(gameLevel0.getGoalSwitch())
        	{
        		levelComplete = true;
        	}
        	
        }
        
      private void updateLevel2()
        {
        	camLocPos = new Vector2f(cam.getLocation().x, cam.getLocation().z);
        	
        	for(int i = 0; i < gameLevel2.puzzleSize(); i++)
        	{
        		if(gameLevel2.getPuzzleNode(i).getType() == 1)
        		{
        			gameLevel2.getPuzzleNode(i).bulletShots(bullets.CheckCollisions(new BoundingBox(gameLevel2.getPuzzleNode(i).getSwitchLoc(), 5, 5,5)));
        		}
        		
        		if(gameLevel2.getPuzzleNode(i).checkFenceCollisions(camLocPos, isStuck))
        		{
        			newLoc.set(new Vector3f(gameLevel2.getPuzzleNode(i).getFencePos().x, (tb.getHeight(gameLevel2.getPuzzleNode(i).getFencePos().x,gameLevel2.getPuzzleNode(i).getFencePos().y)+5), gameLevel2.getPuzzleNode(i).getFencePos().y));
        			
        		}
        		if(gameLevel2.getPuzzleNode(i).getType() == 0)
        		{
        			gameLevel2.getPuzzleNode(i).checkSwitchCollisions(camLocPos);
        		}
        	}
        	gameLevel2.checkSwitchCollisions(new Vector2f(cam.getLocation().x, cam.getLocation().z));
        	gameLevel2.updateLevel(timer.getTimePerFrame());
        	
        	if(gameLevel2.getPuzzleNode(0).getIsSwitched())
        	{
        		isStuck = false;
        	}
        	
        	if(gameLevel2.getGoalSwitch())
        	{
        		levelComplete = true;
        	}
        	
        }
        
     private void updateLevel3()
     {
    	 camLocPos = new Vector2f(cam.getLocation().x, cam.getLocation().z);
     	
     	for(int i = 0; i < gameLevel3.puzzleSize(); i++)
     	{
     		
     			if(gameLevel3.getPuzzleNode(i).getType() == 1)
     			{
     				gameLevel3.getPuzzleNode(i).bulletShots(bullets.CheckCollisions(new BoundingBox(gameLevel3.getPuzzleNode(i).getSwitchLoc(), 5, 5,5)));
     			}
     		
     			if( i == 0)
         		{
     				if(gameLevel3.getPuzzleNode(i).checkFenceCollisions(camLocPos, isStuck))
     				{
     					newLoc.set(new Vector3f(gameLevel3.getPuzzleNode(i).getFencePos().x, (tb.getHeight(gameLevel3.getPuzzleNode(i).getFencePos().x,gameLevel3.getPuzzleNode(i).getFencePos().y)+5), gameLevel3.getPuzzleNode(i).getFencePos().y));
     			
     				}
         		}
     			if( i !=0 )
     			{
     				if(gameLevel3.getPuzzleNode(i).checkFenceCollisions(camLocPos))
     				{
     					newLoc.set(cam.getLocation().subtract(cam.getDirection().clone().mult(3f)));
     				}
     			}
     			if(gameLevel3.getPuzzleNode(i).getType() == 0)
     			{
     				gameLevel3.getPuzzleNode(i).checkSwitchCollisions(camLocPos);
     			}
     		}
     		
      	
     	gameLevel3.checkSwitchCollisions(new Vector2f(cam.getLocation().x, cam.getLocation().z));
     	gameLevel3.updateLevel(timer.getTimePerFrame());
     	
     	if(gameLevel3.getPuzzleNode(0).getIsSwitched())
     	{
     		isStuck = false;
     	}
     	
     	if(gameLevel3.getGoalSwitch())
     	{
     		levelComplete = true;
     	}
         	
        }
     
     private boolean node1; 
     private boolean node2; 
     private boolean node3; 
     private boolean node0; 
     
        private void updateLevel4()
	        {
        		
	        	camLocPos = new Vector2f(cam.getLocation().x, cam.getLocation().z);
	        	
	        	for(int i = 0; i < gameLevel4.puzzleSize(); i++)
	        	{
	        		if(gameLevel4.getPuzzleNode(i).getType() == 1)
	        		{
	        			gameLevel4.getPuzzleNode(i).bulletShots(bullets.CheckCollisions(new BoundingBox(gameLevel4.getPuzzleNode(i).getSwitchLoc(), 5, 5,5)));
	        		}
	        		
	        		if(gameLevel4.getPuzzleNode(i).checkFenceCollisions(camLocPos))
	        		{
	        			newLoc.set(cam.getLocation().subtract(cam.getDirection().clone().mult(3f)));
	        		}
	        		if(gameLevel4.getPuzzleNode(i).getType() == 0)
	        		{
	        			gameLevel4.getPuzzleNode(i).checkSwitchCollisions(camLocPos);
	        		}
	        	}
	        	gameLevel4.checkSwitchCollisions(new Vector2f(cam.getLocation().x, cam.getLocation().z));
	        	
	        	
	        	if(gameLevel4.getGoalSwitch())
	        	{
	        		levelComplete = true;
	        	}
	        	
	        	if(gameLevel4.getPuzzleNode(0).getIsSwitched() || gameLevel4.getPuzzleNode(1).getIsSwitched() || gameLevel4.getPuzzleNode(2).getIsSwitched() ||gameLevel4.getPuzzleNode(3).getIsSwitched() )
	        	{
	        		if(node0 && node1 && node2 && node3)
	        		{
	        			gameLevel4.getPuzzleNode(4).setShowFence(true);
	        			gameLevel4.getPuzzleNode(5).setShowFence(true);
	        			gameLevel4.getPuzzleNode(6).setShowFence(true);
	        			node0 = false;
	        		}
	        	}
	        	
	        	if(gameLevel4.getPuzzleNode(1).getIsSwitched() )
	        	{
	        		if(node1)
	        		{
	        			gameLevel4.getPuzzleNode(7).setShowFence(true);
	        			node1 = false;
	        		}
	        	}
	        	if(gameLevel4.getPuzzleNode(2).getIsSwitched())
	        	{
	        		if(node2)
	        		{
	        			gameLevel4.getPuzzleNode(8).setShowFence(true);
	        			node2 = false;
	        		}   		
	        	}
	        	if(gameLevel4.getPuzzleNode(3).getIsSwitched())
	        	{
	        		if(node3)
	        		{
	        			gameLevel4.getPuzzleNode(9).setShowFence(true);
	        			node3 = false;
	        		}	        		
	        	}
	        	
	        	gameLevel4.updateLevel(timer.getTimePerFrame());
	        	
	        	
        }
        
            private void updateLevel5()
	        {
	        	camLocPos = new Vector2f(cam.getLocation().x, cam.getLocation().z);
	        	
	        	for(int i = 0; i < gameLevel5.puzzleSize(); i++)
	        	{
	        		if(gameLevel5.getPuzzleNode(i).getType() == 1)
	        		{
	        			gameLevel5.getPuzzleNode(i).bulletShots(bullets.CheckCollisions(new BoundingBox(gameLevel5.getPuzzleNode(i).getSwitchLoc(), 5, 5,5)));
	        		}
	        		
	        		if(gameLevel5.getPuzzleNode(i).checkFenceCollisions(camLocPos))
	        		{
	        			newLoc.set(cam.getLocation().subtract(cam.getDirection().clone().mult(3f)));
	        		}
	        		if(gameLevel5.getPuzzleNode(i).getType() == 0)
	        		{
	        			gameLevel5.getPuzzleNode(i).checkSwitchCollisions(camLocPos);
	        		}
	        	}
	        	
	        	
	        	if(gameLevel5.getGoalSwitch())
	        	{
	        		levelComplete = true;
	        	}
	        	
	        	
	        	
	        	if(gameLevel5.getPuzzleNode(7).getIsSwitched())
	        	{
	        		if(node2)
	        		{
	        			gameLevel5.getPuzzleNode(8).setShowSwitch(true);
	        			node2 = false;
	        		}	
	        	}
	        	
	        	if(gameLevel5.getPuzzleNode(8).getIsSwitched())
	        	{
	        		if(node3)
	        		{
	        			gameLevel5.getGoalNode().setShowSwitch(true);
	        			node3 = false;
	        		}	
	        	}
	        	
	        	gameLevel5.checkSwitchCollisions(new Vector2f(cam.getLocation().x, cam.getLocation().z));
	        	gameLevel5.updateLevel(timer.getTimePerFrame());
        }
            private void updateLevel6()
	        {
	        	camLocPos = new Vector2f(cam.getLocation().x, cam.getLocation().z);
	        	
	        	for(int i = 0; i < gameLevel6.puzzleSize(); i++)
	        	{
	        		if(gameLevel6.getPuzzleNode(i).getType() == 1)
	        		{
	        			gameLevel6.getPuzzleNode(i).bulletShots(bullets.CheckCollisions(new BoundingBox(gameLevel6.getPuzzleNode(i).getSwitchLoc(), 5, 5,5)));
	        		}
	        		
	        		if(gameLevel6.getPuzzleNode(i).checkFenceCollisions(camLocPos))
	        		{
	        			newLoc.set(cam.getLocation().subtract(cam.getDirection().clone().mult(3f)));
	        		}
	        		if(gameLevel6.getPuzzleNode(i).getType() == 0)
	        		{
	        			gameLevel6.getPuzzleNode(i).checkSwitchCollisions(camLocPos);
	        		}
	        	}
	        	gameLevel6.checkSwitchCollisions(new Vector2f(cam.getLocation().x, cam.getLocation().z));
	        	gameLevel6.updateLevel(timer.getTimePerFrame());
	        	
	        	if(gameLevel6.getGoalSwitch())
	        	{
	        		levelComplete = true;
	        		//gameOver = true;
	        	}
	        	
        }
            private void updateLevel7()
	        {
	        	camLocPos = new Vector2f(cam.getLocation().x, cam.getLocation().z);
	        	
	        	for(int i = 0; i < gameLevel1.puzzleSize(); i++)
	        	{
	        		if(gameLevel7.getPuzzleNode(i).getType() == 1)
	        		{
	        			gameLevel7.getPuzzleNode(i).bulletShots(bullets.CheckCollisions(new BoundingBox(gameLevel7.getPuzzleNode(i).getSwitchLoc(), 5, 5,5)));
	        		}
	        		
	        		if(gameLevel7.getPuzzleNode(i).checkFenceCollisions(camLocPos))
	        		{
	        			newLoc.set(cam.getLocation().subtract(cam.getDirection().clone().mult(3f)));
	        		}
	        		if(gameLevel7.getPuzzleNode(i).getType() == 0)
	        		{
	        			gameLevel7.getPuzzleNode(i).checkSwitchCollisions(camLocPos);
	        		}
	        	}
	        	gameLevel7.checkSwitchCollisions(new Vector2f(cam.getLocation().x, cam.getLocation().z));
	        	gameLevel7.updateLevel(timer.getTimePerFrame());
	        	
	        	if(gameLevel7.getGoalSwitch())
	        	{
	        		levelComplete = true;
	        	}
	        	
        }
        
                
}